package com.giraffe.device.controller.admin;


import com.giraffe.common.core.domain.R;
import com.giraffe.device.domain.Device;
import com.giraffe.device.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备(Device)表相关的http接口
 *
 * @author makejava
 * @since 2023-02-14 10:39:59
 */
@RestController
@RequestMapping("/v1/device")
@Slf4j
@Api(tags = "产品：Device：产品")
public class DeviceController {
    /**
     * 服务对象
     */
    @Resource
    private DeviceService deviceService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据")
    @GetMapping("{id}")
    public R<Device> findById(@PathVariable @ApiParam(name = "id", value = "主键id") String id) {
        try {
            Device product = this.deviceService.findById(id);
            return R.ok(product);
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 新增数据
     *
     * @param product 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据")
    @PostMapping
    public R<Device> create(@ApiParam(name = "product", value = "产品") @RequestBody Device product) {
        try {
            Device result = this.deviceService.create(product);
            return R.ok(result);
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改数据
     *
     * @param product 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改数据")
    @PatchMapping
    public R<Device> update(@ApiParam(name = "product", value = "产品") @RequestBody Device product) {
        try {
            Device result = this.deviceService.update(product);
            return R.ok(result);
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除数据（单个或者批量）
     *
     * @param ids 主键集合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation(value = "删除数据（单个或者批量）")
    public R<?> delete(@ApiParam(name = "ids", value = "主键集合") @RequestParam("ids") List<String> ids) {
        try {
            this.deviceService.delete(ids);
            return R.ok();
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 启用（单个或者批量）
     *
     * @param ids 主键结合
     * @return 启用结果
     */
    @PatchMapping("enable")
    @ApiOperation(value = "启用（单个或者批量）")
    public R<?> enable(@ApiParam(name = "ids", value = "主键集合") @RequestBody List<String> ids) {
        try {
            this.deviceService.enable(ids);
            return R.ok();
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 禁用（单个或者批量）
     *
     * @param ids 主键结合
     * @return 禁用结果
     */
    @PatchMapping("disable")
    @ApiOperation(value = "禁用（单个或者批量）")
    public R<?> disable(@ApiParam(name = "ids", value = "主键集合") @RequestBody List<String> ids) {
        try {
            this.deviceService.disable(ids);
            return R.ok();
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }
}
